"""
使用搜狗翻译提取出翻译结果和翻译内容
https://fanyi.baidu.com/v2transapi?from=zh&to=en
"""

import requests
import json
class all(object):
    def __init__(self):
        self.url='https://fanyi.baidu.com/v2transapi?from=zh&to=en'
        self.headers = {
            'Accept': '*/*',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Acs-Token': '1683458133254_1683458167177_2Pi2BQ/aB9FjWaRxdDr4w/mV36OfGaCU2B0Kxcy4MReO/EnPtkzZBZnTNZchKNL4Gs70ubz+ekOaj98k6YTr+5GGMD1X19qyrqcP0yjaavJvNjC9nP7+W/pTKJmyC+8JhezNb1noB4H9JGuvmJyrk8AObvAjtLZ/Euu9YM65mvBNnHVtjwloTLzWWzGAmOJf5CbLCbbsN2oP81Od2Cjp3tFfBKs7yXUiGe37xq214g0y+cgGfqzJGOuDmhiJ6tBxJ33csvhdhgG4gCaswnNLI5SmF0rm9ybnC9wmFGYxdNMsjY6/lN0EzfV8FfwZHtVUqRqXPg6NMjBcnpr72TW9nQVRVCk/w52GDXo0rwd114Z8qS6r47FyqhgsvwYiGH5yHZwaphwd62GbogEZlzLXowZS95NcPurlff+cFHlMFgNbbY0krNPwj+EU1daqobxUQrWMVRmOOoQT5vCNuyP6DnUkcdzGJ7ouUR/fs2Rk6kWFwDt2mv7eQcu3iAOWcpDT',
            'Connection': 'keep-alive',
            'Content-Length': '149',
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Cookie': 'BIDUPSID=C1AB1E3B9C76A9DB4642194BE881001A; PSTM=1682588113; BAIDUID=C1AB1E3B9C76A9DBA2819675D7519B5F:FG=1; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BAIDU_WISE_UID=wapp_1683366035224_4; __bid_n=187f06f4e3f58199a64207; FPTOKEN=Ig3dYee3+osu4ieDrSvhElrxPaNDkZK7POAwk8X9rbn4mIMAytUkhu9f5csHApX8ZXEtjZmPY/2kkMde2WdEXm/sx5VWwN1knwY4aBNEnRSgMo4IJCgvZ5qfppPAYZJm/0YqyykS7KGfOXnp2tWP0hJw8Xq4naiiIf5uocka+B3tcwUU7EU64VAqn5xtWOrkR12jeG4GF5zz7HrmaWMK0VsNA1HXyLRrK+w4F5594Yujk9AkZlSXSHxjaVOJQ5dxZyPLeX5KcGdMs3ZKAlU+UC+JLHkttNpC5e6/fUaSRs6Gwfe0INKyqLAphINqSHfwL/Q6Jym/dNJXNwn8y7ioqHGZPbCNydkGY8XgniQdqQjo3SiaSXiZqp8bRsrVxzNUKkbVEpejxkqMnfkn+DhsCGxUrH15cGyTK1zM5zyB7hk4oVLkgcfYG02lzGMbXNL2|cus6SOGrdSekKtrx+Xng/dgiie9n27Sa0sIWXBrcRRY=|10|3ed2b1d13c7f3833b515f22098ffab60; BAIDUID_BFESS=C1AB1E3B9C76A9DBA2819675D7519B5F:FG=1; APPGUIDE_10_0_2=1; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; RT="z=1&dm=baidu.com&si=7b9a3f1d-6d90-4c73-8430-c1b63b35cf4d&ss=lhbuxqpi&sl=8&tt=8tg&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&nu=4em5gyw9&cl=igwx&ld=ihsp&ul=2a9ql&hd=2a9su"; BA_HECTOR=058401agag8hah8k242l0l981i5eqe41m; ZFY=Wv55grt:AbBggMfzig1GrG5g12hanrmxWxcfxFEUKEWg:C; H_PS_PSSID=38515_36545_38529_38469_38468_38375_36807_38486_37709_26350_38545; delPer=0; PSINO=7; BCLID=11151784948076184972; BCLID_BFESS=11151784948076184972; BDSFRCVID=vBDOJexroG07VWbfokQqulF0l_weG7bTDYrEOwXPsp3LGJLVFe3JEG0Pts1-dEu-S2OOogKK3gOTHxLF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; BDSFRCVID_BFESS=vBDOJexroG07VWbfokQqulF0l_weG7bTDYrEOwXPsp3LGJLVFe3JEG0Pts1-dEu-S2OOogKK3gOTHxLF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF=tRAOoC_-tDvDqTrP-trf5DCShUFs-fjRB2Q-XPoO3KtbSx3Pb4nO0J_0h-Dj2pRf5mkf3fbgy4op8P3y0bb2DUA1y4vp0tLeWeTxoUJ2-KDVeh5Gqq-KXU4ebPRiJ-b9QgbO2pQ7tt5W8ncFbT7l5hKpbt-q0x-jLTnhVn0MBCK0HPonHjLWD5OQ3f; H_BDCLCKID_SF_BFESS=tRAOoC_-tDvDqTrP-trf5DCShUFs-fjRB2Q-XPoO3KtbSx3Pb4nO0J_0h-Dj2pRf5mkf3fbgy4op8P3y0bb2DUA1y4vp0tLeWeTxoUJ2-KDVeh5Gqq-KXU4ebPRiJ-b9QgbO2pQ7tt5W8ncFbT7l5hKpbt-q0x-jLTnhVn0MBCK0HPonHjLWD5OQ3f; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1683373279,1683458133; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1683458133; ab_sr=1.0.1_NTdkNDBiMDZmZWRhNDk1YzM4OTlmMDg2NjhiM2U1NjY0NjVlOTI0ZmZhZjU2Y2MzYWNhNWY0NzExZWVjZGE4OGMxZGUxNzI4YjVhYjc3YTY0OTY4YzkzOTU4NjlhZmYzMWZlZDQ5YTlmNTFmZGQ2NWUxYzBkNzNkNjM4ZDA5Mjg5NWExOWVlMTY5ZDA5NTJhODNjM2RlZGIyYjNhMmQ5YQ==',
            'Host': 'fanyi.baidu.com',
            'Origin': 'https://fanyi.baidu.com',
            'Referer': 'https://fanyi.baidu.com/?aldtype=16047',
            'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"',
            'Sec-Fetch-Dest': 'empty',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Site': 'same-origin',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
            'X-Requested-With': 'XMLHttpRequest'
            }

        self.data = {
                'from': 'zh',
                'to': 'en',
                'query': '你好',
                'transtype': 'realtime',
                'simple_means_flag': '3',
                'sign': '232427.485594',
                'token': 'e39fdd3492f4eadba4fd7191ea7d1a25',
                'domain': 'common'
            }
    #获取数据
    def all_post(self):
        response=requests.post(self.url,data=self.data,headers=self.headers)
        if response.status_code == 200:
            resp=json.loads(response.text)
            return resp
        else:
            return None

    #解析数据
    def all_sjjx(self,all):
        ba=all['trans_result']['data'][0]['dst']
        ba2=all['trans_result']['data'][0]['src']
        print(f'翻译的数据是“{ba2}”，翻译后的数据是“{ba}”')

if __name__ == '__main__':
    a_post=all()
    a_post.all_sjjx(a_post.all_post())